//
//  UIControl+Easy.swift
//  QJDecoratingHeadlines
//
//  Created by hudan on 2022/1/21.
//  Copyright © 2022 www.jia.com. All rights reserved.
//

import Foundation
import UIKit

enum EasyLayoutType {
    case left
    case right
    case top
    case bottom
}

extension UIButton {
    func easy(_ title :String?, _ titleColor :UIColor?, _ font :UIFont?, _ imgName :String?, _ target :Any?, _ action :Selector?) -> UIButton {
        if title != nil {
            self.setTitle(title, for: .normal);
        }
        if titleColor != nil {
            self.setTitleColor(titleColor, for: .normal);
        }
        if font != nil {
            self.titleLabel?.font = font;
        }
        if imgName != nil {
            self.setImage(UIImage.init(named: imgName!), for: .normal);
        }
        if target != nil && action != nil {
            self.addTarget(target, action:action!, for: .touchUpInside);
        }
        
        return self;
    }
    
    func easy(_ imgName :String?, _ target :Any?, _ action :Selector?) -> UIButton {
        if imgName != nil {
            self.setImage(UIImage.init(named: imgName!), for: .normal);
        }
        if target != nil && action != nil {
            self.addTarget(target, action:action!, for: .touchUpInside);
        }
        
        return self;
    }
    
    func easyLayout(_ lineSpacing: CGFloat, _ imgPosition: EasyLayoutType) {
        let titleSize: CGSize = titleLabel?.sizeThatFits(.init(width: CGFLOAT_MAX, height: CGFLOAT_MAX)) ?? CGSize.zero
        let imageSize: CGSize = imageView?.image?.size ?? CGSize.zero
        
        if titleSize == .zero || imageSize == .zero {
            titleEdgeInsets = .zero
            imageEdgeInsets = .zero
            return
        }
        
        switch imgPosition {
        case .left:
            titleEdgeInsets = .init(top: 0, left: lineSpacing/2, bottom: 0, right: -lineSpacing/2)
            imageEdgeInsets = .init(top: 0, left: -lineSpacing/2, bottom: 0, right: lineSpacing/2)
            break
        case .right:
            titleEdgeInsets = .init(top: 0, left: -(imageSize.width + lineSpacing/2), bottom: 0, right: imageSize.width + lineSpacing/2)
            imageEdgeInsets = .init(top: 0, left: titleSize.width + lineSpacing/2, bottom: 0, right: -(titleSize.width + lineSpacing/2))
            break
        case .top:
            titleEdgeInsets = .init(top: imageSize.height + lineSpacing/2, left: -imageSize.width, bottom: 0, right: 0)
            imageEdgeInsets = .init(top: 0, left: 0, bottom: titleSize.height + lineSpacing/2, right: -titleSize.width)
            break
        case .bottom:
            titleEdgeInsets = .init(top: -(imageSize.height + lineSpacing/2), left: -imageSize.width, bottom: 0, right: 0)
            imageEdgeInsets = .init(top: 0, left: 0, bottom: -(titleSize.height + lineSpacing/2), right: -titleSize.width)
            break
        }
    }
}

extension UILabel {
    func easy(_ text :String?, _ textColor :UIColor?, _ font :UIFont?) -> UILabel {
        self.text = text;        
        if textColor != nil {
            self.textColor = textColor;
        }
        if font != nil {
            self.font = font;
        }
        return self;
    }
    
    func easy(_ textColor :UIColor?, _ font :UIFont?) -> UILabel {
        if textColor != nil {
            self.textColor = textColor;
        }
        if font != nil {
            self.font = font;
        }
        return self;
    }
}
